﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using University.Workshop.Model;
using System.Collections.ObjectModel;

namespace University.Workshop.DataAccess
{
    public static class CashManagementDataAccess
    {
        public const string ConnString = "Data Source=.\\SQLEXPRESS;Initial Catalog=MedicalDatabase;Integrated Security = True";

        public static CashManagement InsertPayment(CashManagement payment)
        {
            if (payment != null)
            {
                using (SqlConnection conn = new SqlConnection(ConnString))
                {
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "INSERT INTO [CashManagement](DoctorId,PatientId,Concept,Date,Amount,PaymentMethod,Discount) VALUES(@DoctorId,@PatientId,@Concept,@Date,@Amount,@PaymentMethod,@Discount)";
                    cmd.Parameters.AddWithValue("DoctorId", payment.DoctorId);
                    cmd.Parameters.AddWithValue("PatientId", payment.PatientId);
                    cmd.Parameters.AddWithValue("Concept", payment.Concept);
                    cmd.Parameters.AddWithValue("Date", payment.Date);
                    cmd.Parameters.AddWithValue("Amount", payment.Amount);
                    cmd.Parameters.AddWithValue("PaymentMethod", payment.PaymentMethod);
                    //cmd.Parameters.AddWithValue("PaymentState", payment.PaymentState);
                    cmd.Parameters.AddWithValue("Discount", payment.Discount);

                    SqlCommand cmdId = conn.CreateCommand();
                    cmdId.CommandText = "SELECT @@Identity";
                    try
                    {
                        conn.Open();
                        int result = cmd.ExecuteNonQuery();
                        if (result <= 0)
                        {
                            throw new DataAccessException("Error inserting a payment in the database");
                        }
                        return payment;
                    }
                    catch (SqlException ex)
                    {
                        throw new DataAccessException("Error inserting a payment in the database SQL Exception" + ex);
                    }
                }
            }
            else
                return null;
        }
    }
}
